package com.example.administrator.printoutindemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.administrator.printoutindemo.popupwin.PopupwinSelectInventory;
import com.example.administrator.printoutindemo.view.MyImageButton;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.List;


public class StoreListInventoryActivity  extends Activity{
	private ListView store_inventory_listView = null;
	private MyImageButton store_inventory_btn_select = null;
	private Button store_inventory_btnpre = null;
	private Button store_inventory_btnnext = null;
	//private EditText store_inventory_edt_select = null;
	private TextView store_inventory_txtpager = null;
	private List<Inventory> li_inventory = null;
	private PopupwinSelectInventory popupwin_select_inventory = null;

	private Inventory inventory = null;
	private StoreListInventoryAdapter adapter = null;

	ProgressDialog progressDiaog;

	private static final String WEBSERVICE_CONFIG = "webServiceInfo";
	private static final int LOGINSUCCESS = 0;
	private static final int NETWORKERROR = 2;
	private static final int SYSERROR = 3;
	private static final int UPSUCCESS = 4;
	private static final int UPEROOR = 5;


	private final Handler hanlder = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
				case LOGINSUCCESS:

					bindList();

					progressDiaog.dismiss();
					break;

				case NETWORKERROR:

					Toast.makeText(StoreListInventoryActivity.this, "网络或系统异常!",
							Toast.LENGTH_LONG).show();
					progressDiaog.dismiss();
					break;
				case SYSERROR:

					Toast.makeText(StoreListInventoryActivity.this, "系统异常!",
							Toast.LENGTH_LONG).show();
					progressDiaog.dismiss();
					break;

				default:
					progressDiaog.dismiss();
					break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.store_activity_listinventory);
		//store_inventory_edt_select = (EditText)findViewById(R.id.store_inventory_edt_select);
		store_inventory_txtpager = (TextView)findViewById(R.id.store_inventory_txtpager);
		store_inventory_listView = (ListView)findViewById(R.id.store_inventory_listView);
		store_inventory_btn_select = (MyImageButton)findViewById(R.id.store_inventory_btn_select);
		store_inventory_btnpre = (Button)findViewById(R.id.store_inventory_btnpre);
		store_inventory_btnnext = (Button)findViewById(R.id.store_inventory_btnnext);
		inventory = new Inventory();
		adapter = new StoreListInventoryAdapter(this);

		progressDiaog = new ProgressDialog(StoreListInventoryActivity.this);
		progressDiaog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置为圆形进度条
		progressDiaog.setTitle("提示");
		// progressDiaog.setIcon(R.drawable.ic_launcher);//设置进度条的图标
		progressDiaog.setMessage("系统正在努力与服务器联系，请稍后......");
		progressDiaog.setIndeterminate(false);// 设置进度条不明确
		progressDiaog.setCancelable(true);

		progressDiaog.show();
		new Thread(new Runnable() {

			@Override
			public void run() {

				String material = "";

//				if (store_inventory_edt_select != null) {
//					material = store_inventory_edt_select.getText().toString();
//				}

				try {
					li_inventory = callWebServiceGetInfo(material,"1","30");

					if (li_inventory != null) {
						Message m = new Message();
						m.what = StoreListInventoryActivity.LOGINSUCCESS;
						hanlder.sendMessage(m);
					}

				} catch (Exception e) {
					Message m = new Message();
					m.what = StoreListInventoryActivity.SYSERROR;
					hanlder.sendMessage(m);
				}

			}
		}).start();

		//查询
		store_inventory_btn_select.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				progressDiaog = new ProgressDialog(StoreListInventoryActivity.this);
//				progressDiaog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置为圆形进度条
//				progressDiaog.setTitle("提示");
//				// progressDiaog.setIcon(R.drawable.ic_launcher);//设置进度条的图标
//				progressDiaog.setMessage("系统正在努力与服务器联系，请稍后......");
//				progressDiaog.setIndeterminate(false);// 设置进度条不明确
//				progressDiaog.setCancelable(true);
//
//				progressDiaog.show();
//				new Thread(new Runnable() {
//
//					@Override
//					public void run() {
//
//						String material = "";
//
//						if (store_inventory_edt_select != null) {
//							material = store_inventory_edt_select.getText().toString();
//						}
//
//						try {
//							li_inventory = callWebServiceGetInfo(material,"1","30");
//
//							if (li_inventory != null) {
//								Message m = new Message();
//								m.what = StoreListInventoryActivity.LOGINSUCCESS;
//								hanlder.sendMessage(m);
//							}
//
//						} catch (Exception e) {
//							Message m = new Message();
//							m.what = StoreListInventoryActivity.SYSERROR;
//							hanlder.sendMessage(m);
//						}
//
//					}
//				}).start();

				popupwin_select_inventory = new PopupwinSelectInventory(StoreListInventoryActivity.this, null);
				popupwin_select_inventory.setAnimationStyle(R.style.popwin_anim_style);

				popupwin_select_inventory.showAtLocation(
						StoreListInventoryActivity.this.findViewById(R.id.main_tab_container),
						Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 200,200 );

//				popupwin_select_inventory.showAsDropDown(store_inventory_listView);
			}
		});


	}

	private void bindList(){
		adapter.setList(li_inventory);
		store_inventory_listView.setAdapter(adapter);
	}

	private List<Inventory> callWebServiceGetInfo(String material,String startIndex,String endIndex) {

		List<Inventory> result = null;

		try {

			/****************** 调用webService ***********************/
			// 调用.net 的WebService.
			String nameSpace = "http://dyjapp.org/";
			String methodName = "GetListInventory";
			SharedPreferences sPreferencces = getSharedPreferences(
					WEBSERVICE_CONFIG, 0);

			String url = "http://" + sPreferencces.getString("ip", "") + ":"
					+ sPreferencces.getString("Port", "")
					+ "/StoreService.asmx";

			String soapAction = nameSpace + methodName;

			// 设置连接参数
			SoapObject request = new SoapObject(nameSpace, methodName);

			// 增加属性参数。 将相关的函数参数放入到过程中。
			request.addProperty("material", material);
			request.addProperty("startIndex", startIndex);
			request.addProperty("endIndex", endIndex);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);// soap协议版本必须用SoapEnvelope.VER11（Soap
			// V1.1）;

			// 注意：这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice
			// 不指定rpc方式则用true否则要用false
			envelope.dotNet = true;

			// envelope.setOutputSoapObject(request);//设置请求参数
			envelope.bodyOut = request; // enveloper.bodyOut=request 与
			// envelope.setOutputSoapObject(request)
			// 效果相同。
			// step4 创建HttpTransportSE对象
			HttpTransportSE ht = new HttpTransportSE(url);
			// step5 调用WebService
			ht.call(soapAction, envelope); // 关键的一步，很多问题都在这一步调试时出现问题。要么是IIS有问题，要么是ksoap2相关参数没配置好。
			if (envelope.getResponse() != null) {
				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
				// if ("1".equals(response.toString())) {
				String t = response.toString();

				if (!t.equals("")) {
					result = inventory.getListFromJSON(t);
				}

			}
			/****************** 调用webService结束 ***********************/

		} catch (Exception e) {
			// TODO: handle exception
			progressDiaog.dismiss();
		}

		return result;
	}



}